/*
 * @Description:
 * @Author: yyh
 * @Date: 2022-03-06 17:46:04
 * @LastEditors: yyh
 * @LastEditTime: 2022-04-22 00:07:49
 */
//导入数据库
const mysql = require("mysql");

module.exports = {
  //数据库配置
  config: {
    connectionLimit: 1000,
    host: "localhost",
    user: "root",
    password: "123456",
    database: "asset_management_system",
    port: "3306",
  },
  //连接数据库，使用mysql的连接池的方式
  //连接池对象
  sqlConnect: function (sql, sqlArr, callBack) {
    var pool = mysql.createPool(this.config);
    pool.getConnection((err, conn) => {
      if (err) {
        console.log("连接失败！");
        return;
      }
      //事件驱动回调
      conn.query(sql, sqlArr, callBack);
      //释放连接
      conn.release();
    });
  },

  //promise回调
  SySqlConnect: async function (sql, sqlArr) {
    try {
      return await new Promise((resolve, reject) => {
        var pool = mysql.createPool(this.config);
        pool.getConnection((err, conn) => {
          if (err) {
            reject(err);
          } else {
            //事件驱动回调
            conn.query(sql, sqlArr, (err_1, data) => {
              if (err_1) {
                reject(err_1);
              } else {
                resolve(data);
              }
            });
            //释放连接
            conn.release();
          }
        });
      });
    } catch (err_2) {
      console.log(err_2);
    }
  },
};
